home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 4 #1 & #2 / IMG 34 JanFeb 1996.iso / Essentials / MGD4Codeƒ / MGW1Codeƒ / MGWGraphics1.1.c < prev    next >
Text File  |  1995-12-08  |  8KB  |  225 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        MGWGraphics1.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun        \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Graphics utilities for Macintosh GameWriter 1.0.0, a training program…                    \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                                        \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20.  
  21. #include "MGWExterns1.h"
  22.  
  23. extern GDHandle    gThisGDevice;
  24. extern GrafPtr  mainWindow, workCPort;    // Your program may use different names.
  25.                                         // See comments, DumpScreenToWork().
  26.                                         
  27. //==============================================================  Functions
  28.  
  29. //--------------------------------------------------------------  CreateOffScreenBitMap
  30.  
  31. // Checks for sufficient memory, then creates an offscreen bitmap.
  32. // If there is not sufficient memory, alerts the user and quits to Finder.
  33.  
  34. void CreateOffScreenBitMap (Rect *theRect, GrafPtr *offScreen)
  35. {
  36.     GrafPtr        theBWPort;
  37.     BitMap        theBitMap;    
  38.     long        theRowBytes;
  39.     
  40.     theBWPort = (GrafPtr)(NewPtr(sizeof(GrafPort)));
  41.     OpenPort(theBWPort);
  42.     theRowBytes = (long)((theRect->right - theRect->left + 15L) / 16L) * 2L;
  43.     theBitMap.rowBytes = (short)theRowBytes;
  44.     theBitMap.baseAddr = NewPtr((long)theBitMap.rowBytes * 
  45.         (theRect->bottom - theRect->top));
  46.     if (theBitMap.baseAddr == 0L)
  47.         RedAlert(kErrLowMemory);
  48.     theBitMap.bounds = *theRect;
  49.     if (MemError() != noErr)
  50.         RedAlert(kErrLowMemory);
  51.     SetPortBits(&theBitMap);
  52.     ClipRect(theRect);
  53.     RectRgn(theBWPort->visRgn, theRect);
  54.     EraseRect(theRect);
  55.     *offScreen = theBWPort;
  56. }
  57.  
  58. //--------------------------------------------------------------  CreateOffScreenPixMap
  59.  
  60. // Handles the creation of an offscreen pixmap.  Depth is assumed to be that of the…
  61. // current gDevice.  If the allocation fails (low memory, etc.) we quit to Finder.
  62.  
  63. void CreateOffScreenPixMap (Rect *theRect, CGrafPtr *offScreen)
  64. {
  65.     CTabHandle    thisColorTable;
  66.     GDHandle    oldDevice;
  67.     CGrafPtr    newCGrafPtr;
  68.     Ptr            theseBits;
  69.     long        sizeOfOff, offRowBytes;
  70.     OSErr        theErr;
  71.     short        thisDepth;
  72.     
  73.     gThisGDevice = GetMainDevice();
  74.     oldDevice = GetGDevice();
  75.     SetGDevice(gThisGDevice);
  76.     newCGrafPtr = 0L;
  77.     newCGrafPtr = (CGrafPtr)NewPtrClear(sizeof(CGrafPort));
  78.     if (newCGrafPtr != 0L)
  79.     {
  80.         OpenCPort(newCGrafPtr);
  81.         thisDepth = (**(*newCGrafPtr).portPixMap).pixelSize;
  82.         offRowBytes = ((((long)thisDepth * 
  83.                 (long)(theRect->right - theRect->left)) + 15L) >> 4L) << 1L;
  84.         sizeOfOff = (long)(theRect->bottom - theRect->top) * offRowBytes;
  85.         OffsetRect(theRect, -theRect->left, -theRect->top);
  86.         theseBits = NewPtr(sizeOfOff);
  87.         if (theseBits != 0L)
  88.         {
  89.             (**(*newCGrafPtr).portPixMap).baseAddr = theseBits;
  90.             (**(*newCGrafPtr).portPixMap).rowBytes = (short)offRowBytes + 0x8000;
  91.             (**(*newCGrafPtr).portPixMap).bounds = *theRect;
  92.             thisColorTable = (**(**gThisGDevice).gdPMap).pmTable;
  93.             theErr = HandToHand((Handle *)&thisColorTable);
  94.             (**(*newCGrafPtr).portPixMap).pmTable = thisColorTable;
  95.             ClipRect(theRect);
  96.             RectRgn(newCGrafPtr->visRgn, theRect);
  97.             ForeColor(blackColor);
  98.             BackColor(whiteColor);
  99.             EraseRect(theRect);
  100.         }
  101.         else
  102.         {
  103.             CloseCPort(newCGrafPtr);        
  104.             DisposePtr((Ptr)newCGrafPtr);
  105.             newCGrafPtr = 0L;
  106.             RedAlert(kErrLowMemory);
  107.         }
  108.     }
  109.     else
  110.         RedAlert(kErrLowMemory);
  111.     
  112.     *offScreen = newCGrafPtr;
  113.     SetGDevice(oldDevice);
  114. }
  115.  
  116. //--------------------------------------------------------------  KillOffScreenBitMap
  117.  
  118. // Disposes of an offscreen bitmap, and frees the memory for other purposes.
  119.  
  120.  
  121.     void KillOffscreenBitMap (GrafPtr *wasPort)
  122.  
  123.     {
  124.         if (wasPort != nil)
  125.             {
  126.                 // Release the data in wasPort.portPixMap since ClosePort won't do it for us.
  127.                 DisposePtr(((*wasPort)->portBits).baseAddr);
  128.                 // And then, close wasPort
  129.                 ClosePort(*wasPort);
  130.                 *wasPort = nil;
  131.             }
  132.     }
  133.  
  134. //--------------------------------------------------------------  KillOffScreenPixMap
  135.  
  136. // Disposes of an offscreen pixMap, and frees the memory for other purposes.
  137.  
  138.  
  139.     void KillOffscreenPixMap (CGrafPtr *wasPort)
  140.  
  141.     {
  142.         if (*wasPort != nil)
  143.             {
  144.                 // Release the data in wasPort.portPixMap since CloseCPort won't do it for us.
  145.                 DisposePtr(((**(*wasPort)->portPixMap)).baseAddr);
  146.                 // Get rid of its ColorTable since we gave it its own seperate copy.
  147.                 DisposeCTable((*(*wasPort)->portPixMap)->pmTable);
  148.                 // And then, close wasPort.
  149.                 CloseCPort(*wasPort);
  150.                 *wasPort = nil;
  151.             }
  152.     }
  153.  
  154.  
  155. //--------------------------------------------------------------  KillOffScreenPixMap
  156.  
  157. // Disposes of an offscreen pixMap, and frees the memory for other purposes.
  158.  
  159.  
  160. //    void KillOffscreenPixMap (CGrafPtr *wasPort)
  161.  
  162. //    {
  163. //        if (wasPort != nil)
  164. //            {
  165. //                CloseCPort(*wasPort);
  166. //                *wasPort = nil;
  167. //            }
  168. //    }
  169.  
  170. //--------------------------------------------------------------  LoadGraphic
  171.  
  172. // Handy function that loads a PICT graphic, get's its bounds and draws it.
  173. // The port drawn to is assumed the current port.  No scaling is done.
  174.  
  175. void LoadGraphic (short thePictID)
  176. {
  177.     Rect        bounds;
  178.     PicHandle    thePicture;
  179.     
  180.     thePicture = GetPicture(thePictID);            // Load graphic from resource fork.
  181.     if (thePicture == 0L)                        // Check to see if nil (did it load?)
  182.         RedAlert(kErrNoResource);
  183.     HLock((Handle)thePicture);                    // If we made it this far, lock handle.
  184.     bounds = (*thePicture)->picFrame;            // Get a copy of the picture's bounds.
  185.     HUnlock((Handle)thePicture);                // We can unlock the picture now.
  186.     OffsetRect(&bounds, -bounds.left, -bounds.top);    // Offset bounds rect to (0, 0).
  187.     DrawPicture(thePicture, &bounds);            // Draw picture to current port.
  188.     ReleaseResource((Handle)thePicture);        // Dispose of picture from heap.
  189. }
  190.  
  191. //--------------------------------------------------------------  DumpWorkToScreen
  192.  
  193. // Simple handy function that copies the entire work pixmap to the screen.
  194. // Of course, you'll have to rename the CopyBits parameters to fit your own program,…
  195. // …unless you habitually name your main window mainWindow and your offscreen…
  196. // …graphics buffer workCPort. Generally, you'll want to set targetRect to the full…
  197. // …clipping rect of the main window.
  198.  
  199.  
  200. void DumpWorkToScreen (Rect targetRect)
  201. {
  202.     CopyBits(&(((GrafPtr)workCPort)->portBits), 
  203.             &((GrafPtr)mainWindow)->portBits, 
  204.             &targetRect, &targetRect, srcCopy, 0L);
  205. }
  206.  
  207. //--------------------------------------------------------------  DumpScreenToWork
  208.  
  209. // Simple handy function that copies the entire screen to the work pixmap.
  210. // The reverse of the above routine. Very handy routines for when you have alerts,…
  211. // …or anything else that temporarily obscures the main window. Call DumpWorkToScreen()…
  212. // …just before the alert, and DumpScreenToWork immediately after the alert.
  213. // Oh yeah, and use the same targetRect for both routines.
  214.  
  215.  
  216. void DumpScreenToWork (Rect targetRect)
  217. {
  218.     CopyBits(&(((GrafPtr)mainWindow)->portBits), 
  219.             &((GrafPtr)workCPort)->portBits, 
  220.             &targetRect, &targetRect, srcCopy, 0L);
  221. }
  222.  
  223. //------------------------------------------------------------------------------------------\\
  224. //                                    End MGWGraphics1.c                                        \\
  225. //------------------------------------------------------------------------------------------\\